﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using DrugStore.Objects;
using DrugStore.Library;
using DrugStore.FormCollect;
using BLToolkit.Data;

namespace DrugStore.FormCollect
{
    public partial class LogOn : Form
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static bool LoggedOn;
        public static int UserRole;
        public static NguoiSuDung User;
        public static DbManager db = new DbManager();


        public LogOn()
        {
            InitializeComponent();
        }

        private void LogOn_Load(object sender, EventArgs e)
        {
            
        }

        private void bt_LogOn_Click(object sender, EventArgs e)
        {
            if (tb_UserName.Text.ToLower().Length >= 4 && tb_Password.Text.Length >= 4)
            {
                List<NguoiSuDung> listUser = DBFactory.GetListItem<NguoiSuDung>(db);
                if (listUser.Exists(us => us.UserName.ToLower() == tb_UserName.Text.ToLower()
                    && us.PassWord == Util.Encryptor.MD5Hash(tb_Password.Text).ToString()))
                {
                    User = listUser.Where(us => us.UserName.ToLower() == tb_UserName.Text.ToLower()
                    && us.PassWord == Util.Encryptor.MD5Hash(tb_Password.Text).ToString()).FirstOrDefault();
                    MainFormAdmin main = new MainFormAdmin();
                    main.Show();
                    LoggedOn = true;
                    UserRole = 1;
                    this.Close();
                }
            }
            
            if (tb_UserName.Text.ToLower().Length > 0)
            {
                if (tb_UserName.Text.ToLower() == "admin")
                {

                    MainFormAdmin main = new MainFormAdmin();
                    main.DestroyChild("fr_NhapHang");

                    LoggedOn = true;
                    UserRole = 1;
                    this.Close();
                }
                else if (tb_UserName.Text.ToLower() == "banhang")
                {
                    LoggedOn = true;
                    UserRole = 2;
                    this.Close();
                }
                else if (tb_UserName.Text.ToLower() == "nhaphang")
                {
                    LoggedOn = true;
                    UserRole = 3;
                    this.Close();
                }
                else if (tb_UserName.Text.ToLower() == "khambenh")
                {
                    LoggedOn = true;
                    UserRole = 4;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Người dùng không tồn tại");
                }
            }
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LogOn_Enter(object sender, EventArgs e)
        {
            if (tb_UserName.Text.ToLower().Length > 0)
            {
                if (tb_UserName.Text.ToLower() == "admin")
                {
                    LoggedOn = true;
                    UserRole = 1;
                    this.Close();
                }
                else if (tb_UserName.Text.ToLower() == "banhang")
                {
                    LoggedOn = true;
                    UserRole = 2;
                    this.Close();
                }
                else if (tb_UserName.Text.ToLower() == "nhaphang")
                {
                    LoggedOn = true;
                    UserRole = 3;
                    this.Close();
                }
                else if (tb_UserName.Text.ToLower() == "khambenh")
                {
                    LoggedOn = true;
                    UserRole = 4;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Người dùng không tồn tại");
                }
            }
        }
    }
}
